package com.guys8.cemsbackend.controller;

import com.guys8.cemsbackend.DTO.ClassInformation;
import com.guys8.cemsbackend.DTO.Response;
import com.guys8.cemsbackend.DTO.UserInformation;
import com.guys8.cemsbackend.DTO.User_health_nacid;
import com.guys8.cemsbackend.entity.*;
import com.guys8.cemsbackend.entity.Class;
import com.guys8.cemsbackend.service.ClassService;
import com.guys8.cemsbackend.service.impl.*;
import com.guys8.cemsbackend.utils.JWTutil.JWTUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.LinkedList;
import java.util.List;

@RestController
@Api(tags="User-用户信息相关接口")
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserServiceImpl userService;
    @Autowired
    private ClassServiceImpl classService;
    @Autowired
    private ClassUserServiceImpl classUserService;
    @Autowired
    private HealthServiceImpl healthService;
    @Autowired
    private NacidServiceImpl nacidService;

    //    @RequestHeader
//    schoolid = JWTUtil.getUsername(schoolid);

    @GetMapping("/getBySchoolid/")
    @ApiOperation("按Token查找返回用户信息(token)")
    public Response getInfoByToken(@RequestHeader String schoolid){
        schoolid = JWTUtil.getUsername(schoolid);
        return getInfoBySchoolId(schoolid);
    }

    @PostMapping("/getBySchoolid/find/{schoolid}")
    @ApiOperation("按SchoolId查找返回用户信息")
    public Response getInfoBySchoolId(@PathVariable String schoolid){
        User user=userService.getbyschoolid(schoolid);
        if(user == null){
            return Response.restResult(0);
        }
        UserInformation userIn = new UserInformation(user);
        return Response.restResult(1,userIn);
    }

    @PostMapping("/updateUserinformation/{user_name}&&{user_sex}&&{user_academy}&&{user_major}&&{user_posts}&&{user_address}&&{user_avatar}")
    @ApiOperation("输入个人信息并修改(token)")
    public Response changeInfoemation(@RequestHeader String schoolid,@PathVariable String user_name, @PathVariable String user_sex, @PathVariable String user_academy,@PathVariable String user_major,@PathVariable String user_posts,@PathVariable String user_address, @PathVariable Integer user_avatar) {
        schoolid = JWTUtil.getUsername(schoolid);

        User user = userService.getbyschoolid(schoolid);
        if(user == null){
            return Response.restResult(0);
        }
        user.setUserName(user_name);
        user.setUserSex(user_sex);
        user.setUserAcademy(user_academy);
        user.setUserMajor(user_major);
        user.setUserPosts(user_posts);
        user.setUserAddress(user_address);
        user.setExt1(user_avatar);
        userService.updateById(user);
        return Response.restResult(1,new UserInformation(user));
    }

    @PostMapping("/InClassBySchoolid/{classname}")
    @ApiOperation("申请加入班级(token)")
    public Response applayInClass(@RequestHeader String schoolid, @PathVariable String classname){
        schoolid = JWTUtil.getUsername(schoolid);

        Class classs = classService.getbyclassname(classname);
        if(classs == null){
            return Response.restResult(2);
        }
        Integer classid = classs.getClassId();

        User user = userService.getbyschoolid(schoolid);
        if(user==null) return Response.restResult(0);

        ClassUser cstudent = classUserService.getbyuser_class(user.getUserId(),classid);
        if(cstudent !=null&&cstudent.getAuditState()!=2) return Response.restResult(2);
        cstudent = new ClassUser(user.getUserId(), classid, 0);
        if (cstudent==null) {
            classUserService.save(cstudent);
        }else{
            classUserService.updateByMultiId(cstudent);
        }
        return Response.restResult(1);
    }

    @PostMapping("/OutClassBySchoolid/{classname}")
    @ApiOperation("退出班级（不需要审核）(token)")
    public Response OutClass(@RequestHeader String schoolid, @PathVariable String classname){
        schoolid = JWTUtil.getUsername(schoolid);

        Class classs = classService.getbyclassname(classname);
        if(classs == null){
            return Response.restResult(2);
        }
        Integer classid = classs.getClassId();

        User user = userService.getbyschoolid(schoolid);
        if(user==null) return Response.restResult(0);

        ClassUser cstudent = classUserService.getbyuser_class(user.getUserId(),classid);
        if(cstudent ==null) return Response.restResult(2);

        classUserService.deletebyuser_class(user.getUserId(),classid);
        return Response.restResult(1);
    }

    @PostMapping("/getHNBySchoolid/")
    @ApiOperation("按Token查找返回用户健康信息(token)")
    public Response getHNBySchoolId(@RequestHeader String schoolid){
        schoolid = JWTUtil.getUsername(schoolid);

        User user = userService.getbyschoolid(schoolid);
        if(user==null) return Response.restResult(0);
        Nacid nacid = nacidService.getById(user.getLastNacid());
        Health health = healthService.getById(user.getLastHealthid());
        User_health_nacid userhn = new User_health_nacid(health,nacid,user);
        return Response.restResult(1,userhn);
    }

    @PostMapping("/getClassesBySchoolid/")
    @ApiOperation("按Token查找返回用户加入的所有班级信息(token)")
    public Response getClassesBySchoolId(@RequestHeader String schoolid){
        schoolid = JWTUtil.getUsername(schoolid);

        User user = userService.getbyschoolid(schoolid);
        if(user==null) return Response.restResult(0);

        List<ClassUser> classes = classUserService.getClassesbystudent(user.getUserId());
        List<ClassInformation> classesIn = new LinkedList<>();
        for(ClassUser classUser:classes){
            ClassUser teacherinclass = classUserService.getteacherinclass(classUser.getClassId());
            List<ClassUser> studentsinclass = classUserService.getstudentinclass(classUser.getClassId());
            List<UserInformation> users = userService.getstudents(studentsinclass);
            if(user==null) users = new LinkedList<>();
            users.add(0,new UserInformation(userService.getById(teacherinclass.getUserId())));
            classesIn.add(new ClassInformation(classService.getById(classUser.getClassId()),users));
        }
        return Response.restResult(1,classesIn);
    }
}
